<?php

	class LayoutHelper extends AppHelper {
		public $helpers = array(
			'Html',
			'Form',
			'Session',
			'Js'
		);
		
		public function __construct($options = array()) {
			$this->View =& ClassRegistry::getObject('view');
			return parent::__construct($options);
		}
		
		
		public function menu($menuAlias, $options = array()) {
			$_options = array(
				'tag' => 'ul',
				'tagAttributes' => array(),
				'selected' => 'selected',
				'dropdown' => false,
				'dropdownClass' => 'sf-menu',
				'element' => 'menu',
			);
			$options = array_merge($_options, $options);
			
			if (!isset($this->View->viewVars['menus_for_layout'][$menuAlias])) {
				return false;
			}
			$menu = $this->View->viewVars['menus_for_layout'][$menuAlias];
			$output = $this->View->element($options['element'], array(
				'menu' => $menu,
				'options' => $options,
			));
			return $output;
		}
		
		public function categories($options = array()) {
			if (!isset($this->View->viewVars['categories_for_layout'])) {
				return false;
			}
			
			return $this->categoriesList($this->View->viewVars['categories_for_layout']);
		}
		
		function categoriesList($data, $level = 0) {
			$tabs = '' . str_repeat("\t", $level * 2);
			$li_tabs = $tabs . "\t";
			$output = $tabs. '<ul class="categories">'."\n";
			foreach ($data as $key=>$val) {
				$output .= $li_tabs . '<li>' . $this->Html->link($val['Taxonomy']['name'], array('shop' => Configure::read('Shop.slug'), 'controller' => 'products', 'action' => 'collections', $val['Term']['slug']))."\n";
				if (isset($val['children'][0])) {
					$output .= $this->categories($val['children'], $level+1);
					$output .= $li_tabs . '</li>'."\n";
				}
				else {
					$output .= '</li>'."\n";
				}
			}
			$output .= $tabs . '</ul>'."\n";
			return $output;
		}
		
		public function nestedLinks($links, $options = array(), $depth = 1) {
			$_options = array();
			$options = array_merge($_options, $options);
	
			$output = '';
			foreach ($links AS $link) {
				$linkAttr = array(
					'id' => 'link-' . $link['Link']['id'],
					'rel' => $link['Link']['rel'],
					'target' => $link['Link']['target'],
					'title' => $link['Link']['description'],
				);
	
				foreach ($linkAttr AS $attrKey => $attrValue) {
					if ($attrValue == null) {
						unset($linkAttr[$attrKey]);
					}
				}
	
	            // if link is in the format: controller:contacts/action:view
				if (strstr($link['Link']['link'], 'controller:')) {
					$link['Link']['link'] = $this->linkStringToArray($link['Link']['link']);
				}
	
	            // Remove locale part before comparing links
				if (!empty($this->params['locale'])) {
					$currentUrl = substr($this->params['url']['url'], strlen($this->params['locale']));
				} else {
					$currentUrl = $this->params['url']['url'];
				}
	
				if (Router::url($link['Link']['link']) == Router::url('/' . $currentUrl)) {
					$linkAttr['class'] = $options['selected'];
				}
	
				$linkOutput = $this->Html->link($link['Link']['title'], $link['Link']['link'], $linkAttr);
				if (isset($link['children']) && count($link['children']) > 0) {
					$linkOutput .= $this->nestedLinks($link['children'], $options, $depth + 1);
				}
				$linkOutput = $this->Html->tag('li', $linkOutput);
				$output .= $linkOutput;
			}
			if ($output != null) {
				$tagAttr = $options['tagAttributes'];
				if ($options['dropdown'] && $depth == 1) {
					$tagAttr['class'] = $options['dropdownClass'];
				}
				$output = $this->Html->tag($options['tag'], $output, $tagAttr);
			}
			return $output;
	    }

    	public function linkStringToArray($link) {
			$link = explode('/', $link);
			$linkArr = array();
			foreach ($link AS $linkElement) {
				if ($linkElement != null) {
					$linkElementE = explode(':', $linkElement);
					if (isset($linkElementE['1'])) {
						$linkArr[$linkElementE['0']] = $linkElementE['1'];
					} else {
						$linkArr[] = $linkElement;
					}
				}
			}
			if (!isset($linkArr['plugin'])) {
				$linkArr['plugin'] = false;
			}
			
			return $linkArr;
		}

	}